import { FC, useState } from "react";
import styles from "./index.module.scss";
import { Button, Space, Divider, Tag, Modal, message } from "antd";
import {
  CopyOutlined,
  DeleteOutlined,
  EditOutlined,
  ExclamationCircleOutlined,
  LineChartOutlined,
  StarOutlined,
} from "@ant-design/icons";
import { Link, useNavigate } from "react-router-dom";
import {
  duplicateQuestionService,
  updateQuestionService,
} from "../../services/question";
import { useRequest } from "ahooks";

const { confirm } = Modal;

type PropTypes = {
  _id: string;
  title: string;
  isPublished: boolean;
  isStar: boolean;
  isDeleted: boolean;
  answerCount: number;
  createdAt: string;
};

const QuestionCard: FC<PropTypes> = (props) => {
  const { _id, title, isPublished, isStar, answerCount, createdAt } = props;
  const nav = useNavigate();
  const [isStarState, setIsStartState] = useState(isStar);
  const { loading: changeStarLoading, run: handleStar } = useRequest(
    async () => {
      await updateQuestionService(_id, {
        isStar: !isStarState,
      });
    },
    {
      manual: true,
      onSuccess() {
        setIsStartState(!isStarState);
        message.success(`已更新`);
      },
    }
  );
  const { loading: duplicateLoading, run: handleDuplicate } = useRequest(
    async () => await duplicateQuestionService(_id),
    {
      manual: true,
      onSuccess(data) {
        message.success("复制成功");
        const { id = "", _id = "" } = data;
        nav(`/question/edit/${id || _id}`);
      },
    }
  );

  const [isDeletedState, setIsDeletedState] = useState(false);
  const { loading: deleteLoading, run: handleDelete } = useRequest(
    async () =>
      await updateQuestionService(_id, {
        isDeleted: true,
      }),
    {
      manual: true,
      onSuccess() {
        message.success("删除成功");
        setIsDeletedState(true);
      },
    }
  );

  if (isDeletedState) return null;

  const del = () => {
    confirm({
      title: "是否删除该问卷？",
      icon: <ExclamationCircleOutlined />,
      onOk: handleDelete,
    });
  };

  return (
    <div className={styles.container}>
      <div className={styles.title}>
        <div className={styles.left}>
          <Link
            to={isPublished ? `/question/stat/${_id}` : `/question/edit/${_id}`}
          >
            <Space>
              {isStarState && <StarOutlined style={{ color: "red" }} />}
              {title}
            </Space>
          </Link>
        </div>
        <div className={styles.right}>
          <Space>
            {isPublished ? (
              <Tag color="processing">已发布</Tag>
            ) : (
              <Tag>未发布</Tag>
            )}
            <span>答卷：{answerCount}</span>
            <span>{createdAt}</span>
          </Space>
        </div>
      </div>
      <Divider style={{ margin: "12px 0" }} />
      <div className={styles["button-container"]}>
        <div className={styles.left}>
          <Space>
            <Button
              icon={<EditOutlined />}
              type="text"
              size="small"
              onClick={() => nav(`/question/edit/${_id}`)}
            >
              编辑问卷
            </Button>
            <Button
              icon={<LineChartOutlined />}
              type="text"
              size="small"
              onClick={() => nav(`/question/stat/${_id}`)}
              disabled={!isPublished}
            >
              问卷统计
            </Button>
          </Space>
        </div>
        <div className={styles.right}>
          <Space>
            <Button
              icon={
                isStarState ? (
                  <StarOutlined style={{ color: "red" }} />
                ) : (
                  <StarOutlined />
                )
              }
              type="text"
              size="small"
              loading={changeStarLoading}
              onClick={handleStar}
            >
              {isStarState ? "取消标星" : "标星"}
            </Button>
            <Button
              icon={<CopyOutlined />}
              type="text"
              size="small"
              loading={duplicateLoading}
              onClick={handleDuplicate}
            >
              复制
            </Button>
            <Button
              icon={<DeleteOutlined />}
              type="text"
              size="small"
              loading={deleteLoading}
              onClick={del}
            >
              删除
            </Button>
          </Space>
        </div>
      </div>
    </div>
  );
};

export default QuestionCard;
